<?php

namespace app\console\command;

use app\common\model\Vod;
use app\common\model\VodInfo;
use HuaweiCloud\SDK\Vod\V1\Model\ShowAssetMetaResponse;
use HuaweiVod\HuaweiVod;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Log;

class UpdateVodInfo extends Command
{
    protected function configure()
    {
        $this->setName('update_vod_info')
            ->setDescription('update huawei vod info');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->info("execute UpdateVodInfo");
        try {
            $vodList = Vod::where(['status' => Vod::VOD_STATUS_UPLOADED])->select();
            foreach ($vodList as $vod) {
                $this->updateVodInfo($vod['id']);
            }
        } catch (\Exception $exception) {
            Log::error($exception->getMessage());
            Log::error($exception->getTraceAsString());
//            $output->error($exception->getMessage());
        }
    }

    protected function updateVodInfo($vodId)
    {
        $vod = Db::name('vod')->where(['id' => $vodId])->find();
        $result = HuaweiVod::make()->assetInfo($vod['asset_id']);
        if ($result) {
            $result = json_decode($result, true);
            Log::info('result:' . json_encode($result));
            if (!empty($result['asset_info_array'])) {
                $assetInfoArray = $result['asset_info_array'];
                foreach ($assetInfoArray as $assetInfo) {
                    $assetVod = Db::name('vod')->where(['asset_id' => $assetInfo['asset_id']])->find();
                    if ($assetInfo['status'] == "PUBLISHED") {
                        Db::name('vod')->where(['id' => $assetVod['id']])->update(['status' => Vod::VOD_STATUS_PUBLISHED]);
                    }
                    if (!empty($assetInfo['play_info_array'])) {
                        $playInfoArray = $assetInfo['play_info_array'];
                        foreach ($playInfoArray as $playInfo) {
                            $vodInfo = Db::name('vod_info')->where(['asset_id' => $assetInfo['asset_id'], 'quality' => $playInfo['meta_data']['quality']])->find();
                            if (empty($vodInfo)) {
                                $vodInfoId = VodInfo::addVodInfo($vod['video_id'],
                                    $vod['videolist_id'],
                                    $vod['id'],
                                    $playInfo['url'],
                                    $playInfo['play_type'],
                                    $playInfo['meta_data']['codec'],
                                    $playInfo['meta_data']['duration'],
                                    $playInfo['meta_data']['video_size'],
                                    $playInfo['meta_data']['width'],
                                    $playInfo['meta_data']['hight'],
                                    $playInfo['meta_data']['bit_rate'],
                                    $playInfo['meta_data']['frame_rate'],
                                    $playInfo['meta_data']['quality'],
                                    $assetInfo['asset_id']);
                            } else {
                                $attrs['url'] = $playInfo['url'];
                                $attrs['play_type'] = $playInfo['play_type'];
                                $attrs['codec'] = $playInfo['meta_data']['codec'];
                                $attrs['duration'] = $playInfo['meta_data']['duration'];
                                $attrs['video_size'] = $playInfo['meta_data']['video_size'];
                                $attrs['width'] = $playInfo['meta_data']['width'];
                                $attrs['hight'] = $playInfo['meta_data']['hight'];
                                $attrs['bit_rate'] = $playInfo['meta_data']['bit_rate'];
                                $attrs['frame_rate'] = $playInfo['meta_data']['frame_rate'];
                                $attrs['quality'] = $playInfo['meta_data']['quality'];
                                VodInfo::updateVodInfo($vodInfo['id'], $attrs);
                                $vodInfoId = $vodInfo['id'];
                            }
                        }
                    }
                }
            }
        }
    }
}
